package com.example.itshome.activity;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.itshome.R;
import com.example.itshome.adapter.CreatePostRecycleViewAdapter;
import com.example.itshome.entity.MyInteger;
import com.example.itshome.impl.ItemDecorationImpl;
import com.example.itshome.network.AbstractNetworkRequest;
import com.example.itshome.network.SendPostRequest;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static com.example.itshome.util.FileUtil.uriToFileApiQ;

public class CreatePostActivity extends AppCompatActivity {

    private ImageView backImageView;
    private EditText contentEditText;
    private RecyclerView mRecycleView;
    private ImageView createImageView;
    private View locateLayout;
    private List<File> mRecycleViewData;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_post);
        initFind();
        initButton();
        initRecycleView();
    }

    void initFind(){
        backImageView=findViewById(R.id.imv_create_post_page_back);
        mRecycleView=findViewById(R.id.rcv_create_post);
        createImageView=findViewById(R.id.imv_create_post_page_create);
        locateLayout=findViewById(R.id.layout_create_page_add_locate);
        contentEditText=findViewById(R.id.edt_create_page_content);
    }


    private void initButton(){
        backImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        createImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AbstractNetworkRequest request=new SendPostRequest(CreatePostActivity.this)
                        .setText(contentEditText.getText().toString())
                        .setFiles(mRecycleViewData);
                request.start();
            }
        });
        locateLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
    }

    void initRecycleView(){
        mRecycleViewData=new ArrayList<>();
        mRecycleView.setLayoutManager(new GridLayoutManager(this,3));
        mRecycleView.setAdapter(new CreatePostRecycleViewAdapter(this,mRecycleViewData));

        mRecycleView.addItemDecoration(new ItemDecorationImpl(this,10,10));
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == 1) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                //参数一:对应的数据的URI 参数二:使用该函数表示要查找文件的MIME类型
                intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                startActivityForResult(intent, 1);
            } else {
                Toast.makeText(this, "你拒绝打开此权限，无法进行下一步操作！", Toast.LENGTH_SHORT).show();
            }
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (requestCode == 1 && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            //Log.i("TAG", "onActivityResult: " + selectedImage.toString());
            mRecycleViewData.add(uriToFileApiQ(selectedImage,this));
            Objects.requireNonNull(mRecycleView.getAdapter()).notifyItemInserted(mRecycleViewData.size()-1);
            //Log.i("TAG", "onActivityResult: " + uriToFileApiQ(selectedImage,this).getName());
        }
        super.onActivityResult(requestCode, resultCode, data);
    }


}
